package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionService;
import com.heima.feigns.behavior.BehaviorFeign;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {
    /**
     * 添加收藏行为
     *
     * @param dto
     * @return
     */
    @Autowired
    BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
        //1.参数校验
        if(dto == null || dto.getEntryId() == null || dto.getOperation() < 0 || dto.getOperation() > 1 || dto.getType() < 0 || dto.getType() >1 ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"请先登录");
        }

        //2.查询行为实体 主键
        ApBehaviorEntry behavior = behaviorFeign.findByUserIdOrEquipmentId(user.getId(), null);
        if (behavior == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_BEHAVIOR_ENTRY_NOT_EXIST);
        }
        //3.查询收藏行为是否存在
        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery()
                .eq(ApCollection::getArticleId, dto.getEntryId())
                .eq(ApCollection::getEntryId, behavior.getId()));

        //4.不存在，判断是否是收藏行为
        if (apCollection == null && dto.getOperation() == 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"暂无收藏");
        }

        //5.已存在，判断重复收藏
        if (apCollection != null && dto.getOperation() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"请不要重复收藏");
        }

        //保存收藏
        if (apCollection == null && dto.getOperation() == 0){
            apCollection = new ApCollection();
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setCollectionTime(new Date());
            apCollection.setPublishedTime(dto.getPublishedTime());
            apCollection.setEntryId(behavior.getId());
            apCollection.setType(dto.getType());
            apCollection.setOperation(dto.getOperation());
            save(apCollection);
        }else {
            //或者取消收藏
            apCollection.setOperation(dto.getOperation());
            updateById(apCollection);
        }

        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
